package com.linln.modules.business.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linln.common.constant.AdminConst;
import com.linln.common.data.MybatisPage;
import com.linln.component.shiro.ShiroUtil;
import com.linln.modules.business.dao.MapperSalary;
import com.linln.modules.business.domain.Salary;
import com.linln.modules.system.domain.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author Administrator
 * @date 2021/11/13
 */
@Service
public class SalaryServiceImpl extends ServiceImpl<MapperSalary, Salary>  {


    @Autowired
    MapperSalary mapperSalary;
    /**
     * 个人工资明细
     *
     * @return
     */

    public IPage<Salary> getMySalaryDetail(Salary salary) {
        IPage<Salary> iPage = MybatisPage.pageRequest();
        Long userId = ShiroUtil.getSubject().getId();
        Set<Role> roles = ShiroUtil.getSubjectRoles();
        Map<String,Object> query = new HashMap<>(8);
        //如果是管理员,则可以查看所有信息

        final boolean[] hasAdminRole = {false};
        roles.forEach(role -> {
            if (role.getName().equals(AdminConst.ADMIN_ROLE_NAME) || role.getName().equals("manager")) {
                hasAdminRole[0] = true;
                return;
            }});

        //不是管理员角色
        if (!hasAdminRole[0]) {
            query.put("userId", userId);
        }

        query.put("payDateStr", salary.getPayDateStr());

        iPage = mapperSalary.selectSalaryList(iPage, query);
        return iPage;
    }

    /**
     * 删除工资记录
     *
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public boolean delSalary(String id) {
        return this.removeById(id);
    }
}